screen_size = (640, 480)
flags = 0
frame_time = 50

import os, sys
from os.path import dirname as d

import pygame
pygame.init()

from pygame.locals import *
import data

import albow
from albow.screen import Screen
from albow.text_screen import TextScreen
from albow.shell import Shell
from albow.resource import get_font, get_image
from albow.controls import Label, Button, Image
from albow.layout import Row, Column, Grid

class CommsException(Exception):
    "used for passing information from menu."

class LevelScreen(Screen):

    def __init__(self, shell, bg, levels):
        Screen.__init__(self, shell)
        self.shell = shell
        f1 = get_font(24, 'Vera.ttf')
        title = Label('*Levels*', font=f1)

        def execute(text):
            return Button(text, action=lambda:self.quit(text))
        menu = Column([execute(name) for name, l in levels] + [
            Button("Back", action=self.go_back),
            ], align='l')

        contents = Column([title, menu,], align='l', spacing=20)

        self.add_centered(contents)
        contents.topleft = (300, 300)
        self.bg = bg

    def go_back(self):
        self.parent.show_menu()

    def quit(self, payload):
        raise CommsException(payload)

    def draw(self, surface):
        surface.blit(self.bg, (0,0))

class MenuScreen(Screen):

    def __init__(self, shell, bg):
        Screen.__init__(self, shell)
        self.shell = shell
        f1 = get_font(24, 'Vera.ttf')
        title = Label("*featherinyourcap*", font=f1)
    
        def menu_button(text, screen):
            return Button(text, action=lambda:shell.show_screen(screen))
        menu = Column([
            menu_button("Play", shell.level_menu),
            menu_button("Help", shell.text_screen),
            Button("Quit", action=lambda:self.quit('quit')),
            ], align='l')
        contents = Column([title, menu,
                ], align='l', spacing=20)

        self.add_centered(contents)
        contents.topleft = (300, 300)
        self.bg = bg

    def draw(self, surface):
        surface.blit(self.bg, (0,0))

    def quit(self, payload):
        raise CommsException(payload)


class DemoShell(Shell):

    def __init__(self, display, bg, levels):
        Shell.__init__(self, display)
        self.level_menu = LevelScreen(self, bg, levels)
        self.text_screen = TextScreen(self, "instruction.txt")
        self.text_screen._draw = self.text_screen.draw
        helpbg, _ = data.load_image('bg.png', None)
        def draw(surface):
            display = pygame.display.get_surface()
            newbg = helpbg.copy()
            display.blit(newbg, (0, 0))
            self.text_screen._draw(newbg)
            surface.blit(newbg, (0, 0))
        self.text_screen.draw = draw
        self.menu_screen = MenuScreen(self, bg)
        self.set_timer(frame_time)
        self.show_menu()

    def show_menu(self):
        self.show_screen(self.menu_screen)

def main(display, bg, levels):
    shell = DemoShell(display, bg, levels)
    shell.run()


